package com.e2oto.seller.modules.seller.presenter;

import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.common.EasyAES;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.base.BasePresenter;
import com.e2oto.seller.common.base.LoadingDialog;
import com.e2oto.seller.common.utils.RetrofitUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.seller.bean.SpecBean;
import com.e2oto.seller.modules.seller.views.GoodsSpecListView;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ZhangLiQiang on 2017/11/13.
 */

public class GoodsSpecListPresenter extends BasePresenter<GoodsSpecListView> {

    public void addSpec(String seller_id, String goods_id, String store_total, List<SpecBean> spec_array) {

        final LoadingDialog loadingDialog = new LoadingDialog(getContext());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("seller_id", seller_id);
        jsonObject.put("goods_id", goods_id);
        jsonObject.put("store_total", store_total);
        jsonObject.put("spec_array", spec_array.toArray());
        String param=EasyAES.encrypt(jsonObject);

        RetrofitUtils.getApiService().addGoodsSecondStep(param, SellerApp.versionName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        loadingDialog.show();
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        if (baseBean.getCode() == 200) {
                            getView().addSpecCallback();
                        } else {
                            getView().showMessage(baseBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        loadingDialog.dismiss();
                        getView().showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        loadingDialog.dismiss();
                    }
                });
    }
}
